package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐接口")
public class SetmealController {

    @Autowired
    private SetmealService setmealServicel;

    @PostMapping("/status/{status}")
    @ApiOperation("套餐状态修改")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result setSetmealStatus(@PathVariable Integer status, Long id){
        log.info("[套餐状态修改] stutas:{},id:{}",status,id);
        setmealServicel.setSetmealStatus(status,id);
        return Result.success();
    }

    /**
     * 更新套餐信息
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @ApiOperation("更新套餐信息")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result updateSetmealList(@RequestBody SetmealDTO setmealDTO){
        log.info("[更新套餐信息] setmealDTO", setmealDTO);
        setmealServicel.updateSetmealList(setmealDTO);
        return Result.success();
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> findSetmealById(@PathVariable Long id){
        log.info("[根据id查询套餐] id", id);
        SetmealVO setmealVO = setmealServicel.findSetmealById(id);
        return Result.success(setmealVO);
    }

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO){
        log.info("[新增套餐],setmealDTO", setmealDTO);
        setmealServicel.addSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> pageSetmeal(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("[分页查询] setmealPageQueryDTO", setmealPageQueryDTO);
        PageResult page = setmealServicel.pageSetmeal(setmealPageQueryDTO);
        return Result.success(page);
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteSetmeal(@RequestParam List<Long> ids){
        log.info("[批量删除套餐] ids" ,ids);
        setmealServicel.deleteSetmeal(ids);
        return Result.success();
    }
}
